package com.cskaoyan.mall.commons.exception.yyr;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.exception.ProcessException;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.yyr.AbstractResponse;

/**
 * @Description：
 * @Author: 杨涌仁
 * @Date: 2022/08/25/0025 10:19
 */
public class ExceptionUtil {

    /**
     * 将下层抛出的异常转换成resp返回码
     *
     * @param e Exception
     */
    public static AbstractResponse handlerException4biz(AbstractResponse response, Exception e) throws Exception {
        Exception ex = null;
        if (!(e instanceof Exception)) {
            return null;
        }
        if (e instanceof ValidateException) {
            response.setCode(((ValidateException) e).getErrorCode());
            response.setMsg(e.getMessage());
        } else if (e instanceof ProcessException) {
            response.setCode(((ProcessException) e).getErrorCode());
            response.setMsg(e.getMessage());
        } else if (e instanceof BizException) {
            response.setCode(((BizException) e).getErrorCode());
            response.setMsg(e.getMessage());
        } else if (e instanceof Exception) {
            // 处理不了，抛出去让调用方处理
            throw e;
        }
        return response;
    }
}
